package com.data.service.entity;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.data.common.aspect.annotation.Dict;
import com.data.common.util.SpringContextHolder;
import com.data.service.EventLogService;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.context.ApplicationContext;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.io.Serializable;
import java.util.Date;
import java.util.function.Supplier;

/**
 * <p>
 * EventLogBO
 * </p>
 *
 * @author li
 * @version 1.0
 * @date 2022-02-11 17:30
 * @since 1.0
 */
@Slf4j
@Builder
@Data
public class EventLogFeignBO implements Serializable {

    private static final long serialVersionUID = 1L;

    /**主键*/
    @TableId(type = IdType.ASSIGN_ID)
    @ApiModelProperty(value = "主键")
    private String id;
    /**创建人*/
    @ApiModelProperty(value = "创建人")
    private String createBy;
    /**创建日期*/
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @ApiModelProperty(value = "创建日期")
    private Date createTime;
    /**更新人*/
    @ApiModelProperty(value = "更新人")
    private String updateBy;
    /**更新日期*/
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @ApiModelProperty(value = "更新日期")
    private Date updateTime;
    /**所属部门*/
    @ApiModelProperty(value = "所属部门")
    private String sysOrgCode;
    /**事件类型*/
    @Excel(name = "事件类型", width = 15, dicCode = "biz_event_type")
    @Dict(dicCode = "biz_event_type")
    @ApiModelProperty(value = "事件类型")
    private String eventType;
    /**摘要描述*/
    @Excel(name = "摘要描述", width = 15)
    @ApiModelProperty(value = "摘要描述")
    private String eventDesc;
    /**请求参数*/
    @Excel(name = "请求参数", width = 15)
    @ApiModelProperty(value = "请求参数")
    private String reqArgs;
    /**原始值*/
    @Excel(name = "原始值", width = 15)
    @ApiModelProperty(value = "原始值")
    private String fromInfo;
    /**ip地址*/
    @Excel(name = "ip地址", width = 15)
    @ApiModelProperty(value = "ip地址")
    private String ipAdr;
    /**业务ID*/
    @Excel(name = "业务ID", width = 15)
    @ApiModelProperty(value = "业务ID")
    private String bizId;
    /**业务名称*/
    @Excel(name = "业务名称", width = 15)
    @ApiModelProperty(value = "业务名称")
    private String bizName;
    /**经、维度*/
    @Excel(name = "经、维度", width = 15)
    @ApiModelProperty(value = "经、维度")
    private String lngLat;
    /**详细地址*/
    @Excel(name = "详细地址", width = 15)
    @ApiModelProperty(value = "详细地址")
    private String detailAddr;

    /**
     * 发送通知到 log
     *
     * @param supplier 请求info
     */
    public static void sendEventLogAdvice(Supplier<EventLogFeignBO> supplier) {
        try {
            ApplicationContext context = SpringContextHolder.getApplicationContext();
            ThreadPoolTaskExecutor executor = context.getBean(ThreadPoolTaskExecutor.class);
            executor.execute(() -> {
                EventLogService eventLogService = context.getBean(EventLogService.class);
                log.error("发送 error log -> eventLog json={}", JSONObject.toJSONString(supplier.get()));
                eventLogService.insert(supplier.get());
            });
        } catch (Exception e) {
            log.error("gateway 日志异常 通知也失败了 GameOver!",e);
        }

    }

}
